package model;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

/**
 * 
 * @author Sonny Sandberg
 */
public class Telephone 
{
    private String number;
    private model.CustomerName customer;
    private ArrayList<ActionListener> listeners;

    public Telephone(String number, model.CustomerName customer)
    {
        this.number = number;
        this.customer = customer;
        listeners = new ArrayList();
        notifyListeners();
    }
    
    public Telephone()
    {
        listeners = new ArrayList();
        reset();
    }
    
    public void reset()
    {
        number = "";
        customer = null;
        notifyListeners();
    }

    public String getNumber()
    {
        return number;
    }

    public void setNumber(String number)
    {
        this.number = number;
        notifyListeners();
    }

    public CustomerName getCustomer()
    {
        return customer;
    }

    public void setCustomer(CustomerName customer)
    {
        this.customer = customer;
        notifyListeners();
    }
    
    public boolean isCompleted()
    {
        boolean answer = false;
        
        if (number.length() > 0 && customer.isCompleted() == true)
        {
            answer = true;
        }
        
        return answer;
    }
    
    @Override
    public String toString()
    {
        String text = "";
        
        text = String.format("%s\n%s", number, customer.toString());
        
        return text;
    }
    
    public void addListener(ActionListener listener) 
    {
        listeners.add(listener);
    }

    private void notifyListeners() 
    {
        ActionEvent event = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Something changed");
        for (ActionListener listener : listeners) 
        {
            listener.actionPerformed(event);
        }
    }
}